#!/usr/bin/env python3

import re
from os.path import join
from subprocess import check_call, check_output
from tempfile import TemporaryDirectory


def print_packages(packages, heading):
    print(f'{heading}:')
    print('  ', end='')
    print('\n  '.join(packages))


# would  be nice if there was a cleaner way to get this, but I've not found a
# more reliable one.
with open('setup.py') as fh:
    match = re.search(r"name='(?P<pkg>[\w-]+)',", fh.read())
    if not match:
        raise Exception('failed to determine our package name')
    our_package_name = match.group('pkg')
    print(f'our_package_name: {our_package_name}')

with TemporaryDirectory() as tmpdir:
    check_call(['python3', '-m', 'venv', tmpdir])

    # base needs
    check_call([join(tmpdir, 'bin', 'pip'), 'install', '.'])
    frozen = check_output([join(tmpdir, 'bin', 'pip'), 'freeze'])
    frozen = set(frozen.decode('utf-8').strip().split('\n'))

    # dev additions
    check_call([join(tmpdir, 'bin', 'pip'), 'install', '.[dev]'])
    dev_frozen = check_output([join(tmpdir, 'bin', 'pip'), 'freeze'])
    dev_frozen = set(dev_frozen.decode('utf-8').strip().split('\n')) - frozen

# pip installs the module itself along with deps so we need to get that out of
# our list by finding the thing that was file installed during dev
frozen = sorted([p for p in frozen if not p.startswith(our_package_name)])
dev_frozen = sorted(
    [p for p in dev_frozen if not p.startswith(our_package_name)]
)

# special handling for click until python 3.9 is gone due to it dropping
# support for active versions early
i = [i for i, r in enumerate(dev_frozen) if r.startswith('click==')][0]
dev_frozen = (
    dev_frozen[:i]
    + [
        "click==8.1.8; python_version<'3.10'",
        f"{dev_frozen[i]}; python_version>='3.10'",
    ]
    + dev_frozen[i + 1 :]
)

# special handling for greenlet until python 3.11 is gone due to it dropping
# support for active versions early
i = [i for i, r in enumerate(dev_frozen) if r.startswith('greenlet==')][0]
dev_frozen = (
    dev_frozen[:i]
    + [
        "greenlet==2.0.2; python_version<'3.12'",
        f"{dev_frozen[i]}; python_version>='3.12.0rc0'",
    ]
    + dev_frozen[i + 1 :]
)

print_packages(frozen, 'frozen')
print_packages(dev_frozen, 'dev_frozen')

with open('requirements.txt', 'w') as fh:
    fh.write('\n'.join(frozen))
    fh.write('\n')

with open('requirements-dev.txt', 'w') as fh:
    fh.write('\n'.join(dev_frozen))
    fh.write('\n')
